<template>
  <div class="login-container">
    <div class="login-wrapper">
      <div class="login-banner">
        <img src="~@/assets/images/login-side.png" alt="">
      </div>
      <div class="login-box">
        <div class="login-title">
          <h2>装企CRM管理系统</h2>
          <small>Customer Relationship Management</small>
        </div>
        <div class="login-tabs">
          <a :class="{'active': loginType}" @click="loginType=true">账号登录</a>
          <a :class="{'active': !loginType}" @click="loginType=false">微信登录</a>
        </div>
        <!-- 账号登录 -->
        <el-form v-if="loginType" ref="formRef" :model="formData" :rules="formRules" size="large"
                 @submit.native.prevent @keyup.enter.native="handleLogin">
          <!-- 账号 -->
          <el-form-item prop="username">
            <el-input v-model="formData.username" placeholder="输入账户名">
              <template #prefix>
                <el-icon class="el-input__icon"><User/></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input v-model="formData.password" type="password" placeholder="输入密码" show-password>
              <template #prefix>
                <el-icon class="el-input__icon"><Lock/></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-button class="login-btn" type="primary"  @click="handleLogin">登录</el-button>
        </el-form>
        <!-- 微信登录 -->
        <div v-if="!loginType" class="login-wx">
          <img src="~@/assets/images/login-wx.png" alt="微信二维码">
          <p>请使用手机微信扫描二维码登录</p>
          <el-button type="primary" link><el-icon class="el-icon--left"><Refresh/></el-icon>刷新</el-button>
        </div>
      </div><!--/.login-box-->
    </div><!--/.login-wrapper-->
  </div><!--/.login-container-->
  <div class="login-footer">
    Customer Relationship Management
  </div>
</template>

<script setup>
import {ref} from 'vue'
import {ElMessage} from 'element-plus'
import router from '@/router'
import request from '@/utils/request'

const loginType = ref(true)
const loading = ref(false)
const formRef = ref()
const formData = ref({
  username: undefined,
  password: undefined
})
const formRules = ref({
  username: [{required: true, message: '请输入账号'}],
  password: [{required: true, message: '请输入密码'}]
})


const handleLogin = () => {
  formRef.value.validate((valid) => {
    if (!valid) {
      ElMessage.error('请正确填写参数')
      return
    }
    loading.value = true
    request.post('/api/crm/user/login', formData.value)
        .then(res => {
          loading.value = false
          ElMessage.success('登录成功')
          localStorage.setItem('token', res.data.token)
          localStorage.setItem('uname', res.data.username)
          localStorage.setItem('uid', res.data.id)
          localStorage.setItem('avatar',res.data.avatar)
          localStorage.setItem('perms',JSON.stringify(res.data.perms))
          let redirect = router.currentRoute.value.query?.redirect
          router.push({path: redirect || '/'})
        });
  })
}
</script>

<style scoped>
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100vh;
  background-color: #2191f7;
}

.login-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-banner {
  margin-right: 50px;
}

.login-banner > img {
  width: 380px;
  height: 380px;
}

.login-box {
  position: relative;
  box-sizing: border-box;
  width: 360px;
  height: 400px;
  padding: 50px;
  background-color: #bddefd;
  border-radius: 1rem;
}

.login-title {
  position: relative;
  margin-bottom: 10px;
  text-align: center;
  white-space: nowrap;
}

.login-title h2 {
  margin: 0;
  font-size: 24px;
  font-weight: 400;
  color: #0066cc;
  letter-spacing: 0.1rem;
}

.login-title small {
  font-size: 12px;
  font-weight: 400;
  color: rgba(0, 102, 204, 0.7);
}

.login-tabs {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 25px;
}

.login-tabs a {
  flex: 1;
  padding: 10px 20px;
  font-size: 16px;
  color: #5a5a5a;
  text-align: center;
  text-decoration: none;
  border-bottom: 2px solid transparent;
  cursor: pointer;
}

.login-tabs a.active {
  color: #409eff;
  border-bottom-color: #409eff;
}

.login-btn {
  width: 100%;
  font-size: 16px;
  letter-spacing: .1rem;
}

.login-wx {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.login-wx img {
  width: 120px;
}

.login-wx p {
  margin: 15px 0 5px;
  font-size: 12px;
  color: #666;
}

.login-footer {
  position: fixed;
  bottom: 25px;
  width: 100%;
  text-align: center;
  color: #d7d7d7;
}

@media (max-width: 991px) {
  .login-banner, .login-footer {
    display: none;
  }
}

@media (max-width: 767px) {
  .login-container {
    min-height: 400px;
  }
}
</style>
